package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.enumeration.OperationType;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


@Mapper
public interface OrderMapper {

    @AutoFill(OperationType.INSERT)
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) values" +
            " (#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    void save(Orders orders);


    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    Page<List<OrderVO>> pageQueryOrder(Integer status);

    OrderVO queryById(Integer id);

    /**
     * 根据订单id查询订单
     * @param id
     * @return
     */
    Orders getById(Long id);

    /**
     * 根据id查询订单详情
     * @param id
     * @return
     */
    OrderVO getVoById(Long id);

    /**
     * 取消订单
     * @param id
     * @return
     */
    @Update("update orders set status = 6 where id = #{id}")
    void cancelOrder(@Param("id") Integer id);

    /**
     * 根据订单ID查询订单明细表
     * @param id
     * @return
     */
    @Select("select * from order_detail where order_id = #{id}")
    List<OrderDetail> queryOrdersById(@Param("id") Integer id);


    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select `status`,count(*) as num from `orders`  where `status` in (2,3,4) group by `status`")
    List<Map> countStatusByGroup();


    @Update("update orders set status = 4 where id = #{id}")
    void updateId(Long id);


    @Update("update orders set status = 5 where id = #{id}")
    void updateIds(Long id);
}
